using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;

public partial class Portal_FaleConosco : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string script = "";

        try
        {            
            if (this.txtNome.Text.Equals(""))
            {
                script = "<script>alert('Nome vazio!');</script>";
                ClientScript.RegisterStartupScript(Type.GetType("System.String"), "alert", script);

                txtNome.Focus();
            }
            else if (this.txtTelefone.Text.Equals(""))
            {
                script = "<script>alert('Telefone vazio!');</script>";
                ClientScript.RegisterStartupScript(Type.GetType("System.String"), "alert", script);

                txtTelefone.Focus();
            }
            else if (this.txtEmail.Text.Equals(""))
            {
                script = "<script>alert('E-mail vazio!');</script>";
                ClientScript.RegisterStartupScript(Type.GetType("System.String"), "alert", script);

                txtEmail.Focus();
            }
            else
            {
                string subject = "[" + cmbAssunto.SelectedItem.Text + "] " + txtNome.Text.Trim(); 
                string to = "andersonmoyses@gmail.com";
                string from = txtEmail.Text.Trim();
                string telefone = txtTelefone.Text.Trim();

                string body = "";

                body += "<html>";
                body += "   <head>";
                body += "       <link href='../styles/StyleMenu.css' type='text/css' rel='STYLESHEET' />";
                body += "       <link href='../styles/Styles.css' type='text/css' rel='STYLESHEET' />";
                body += "   </head>";
                body += "   <body class='texto'>";
                body += "       <b>Enviado em:</b>&nbsp;" + DateTime.Now.ToString("dd/MM/yyyy HH:mm") + "<br />";
                body += "       <b>Origem:</b>&nbsp;" + from + "<br />";
                body += "       <b>Telefone:</b>&nbsp;" + telefone + "<br />";
                body += "       <b>Assunto:</b>&nbsp;" + subject + "<br />";                
                body += "       <br />";
                body += "       <br />";
                body += "       <table class='texto' border='0' cellspacing='0' cellpadding='0' width='100%'>";
                body += "           <tr>";
                body += "               <td align='justify'>";
                body += "                   " + txtAssunto.Text;
                body += "               </td>";
                body += "           </tr>";
                body += "       </table>";
                body += "   </body>";
                body += "</html>";

                Boolean testeEmail = EnviaEmail("andersonmoyses@gmail.com", to, subject, body);

                if (!testeEmail)
                {
                 script = "<script>alert('Erro ao enviar e-mail!');</script>";
                 ClientScript.RegisterStartupScript(Type.GetType("System.String"), "alert", script);
                }
                else 
                {
                    script = "<script>alert('E-mail enviado com sucesso!');</script>";
                    ClientScript.RegisterStartupScript(Type.GetType("System.String"), "alert", script);
                    txtAssunto.Text = "";
                    txtEmail.Text = "";
                    txtNome.Text = "";
                    txtTelefone.Text = "";                    
                }
            }
        }
        catch (Exception err)
        {

            script = "<script>alert('Erro Ocorrido!\n\n'" + err.ToString() + "')</script>";
            ClientScript.RegisterStartupScript(Type.GetType("System.String"), "alert", script);
        }                
    }    
    protected Boolean EnviaEmail(string strRemetente, string strDestinatarios, string strAssunto, string strMensagem)
    {
        MailMessage oEmail = new MailMessage();

        try
        {
            //remetente do email            
            oEmail.From = new MailAddress(strRemetente);

            //destinatario do email
            if (strDestinatarios.Contains(";"))
            {
                foreach (String destinatario in strDestinatarios.Split(';'))
                {
                    if (!destinatario.Equals(""))
                        oEmail.To.Add(destinatario.Trim());
                }
            }
            else
                oEmail.To.Add(strDestinatarios.Trim());

            //prioridade de envio
            oEmail.Priority = MailPriority.High;

            //define o formato do email
            oEmail.IsBodyHtml = true; 

            //define o assunto do email
            oEmail.Subject = strAssunto;

            //define a mensagem principal do email
            oEmail.Body = strMensagem;

            //Para evitar problemas com caracteres especiais configuramos o Charset
            oEmail.SubjectEncoding = Encoding.GetEncoding("ISO-8859-1");
            oEmail.BodyEncoding = Encoding.GetEncoding("ISO-8859-1");

            //cria o objeto SMTP
            SmtpClient oSmtp = new SmtpClient();            

            oSmtp.Host = "smtp.gmail.com";
            oSmtp.Port = 587;
            oSmtp.EnableSsl = true;

            oSmtp.Credentials = new System.Net.NetworkCredential(strRemetente, "ama2102");
            oSmtp.Timeout = 100000;

            //envia o email
            oSmtp.Send(oEmail);
            return true;
        }
        catch (Exception ex)
        {
            string teste = ex.Message.ToString();
            return false;
        }
        finally
        {
            oEmail.Dispose();
        }
    }
}
